package com.leon.programPeral.ch2.ext1;

import com.alibaba.fastjson.JSONObject;

import java.util.*;

public class OnlyWordAndDirClient
{
	public static List<String> dic = new ArrayList<>();

	static
	{
		dic.add("add");
		dic.add("tops");
		dic.add("stop");

	}

	public static void main(String[] args)
	{
		OnlyWordAndDir o = new OnlyWordAndDir();
		o.chang("stop", dic);
		//		System.out.println(o.cha("stops"));
	}
}

class OnlyWordAndDir
{
	/**
	 *
	 * @param word 所要查的单词
	 * @param dic 给定的字典
	 * @return 所有的变位词
	 */
	public List<String> chang(String word, List<String> dic)
	{
		String w = cha(word);
		dic.stream().filter((a) -> cha(a).equals(w)).forEach(a-> System.out.println(a));
		System.out.println(JSONObject.toJSONString(dic));
		return null;
	}

	/**
	 * 转换为特征词
	 * @param word stops
	 * @return o1p1s2t1
	 */
	public static String cha(String word)
	{
		Map<Character, Integer> map = new TreeMap<>();
		for (int i = 0; i < word.length(); i++)
		{
			if (!map.containsKey(word.charAt(i)))
			{
				map.put(word.charAt(i), 1);
			}
			else
			{
				map.put(word.charAt(i), map.get(word.charAt(i)) + 1);
			}
		}
		StringBuilder sb = new StringBuilder();
		for (Map.Entry<Character, Integer> me : map.entrySet())
		{
			sb.append(me.getKey());
			sb.append(me.getValue());
		}
		return sb.toString();
	}
}

